# -*- coding: utf-8 -*-
from common import orm
from common.lottery.cyclical import ACTIVITY_STATUS


class Activity(orm.Model):
    __tablename__ = "bj_pk10"
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)          # 期号
    number = orm.Column(orm.VARCHAR)        # 开奖结果
    reference = orm.Column(orm.TEXT)        # 爬取结果页url
    status = orm.Column(orm.SmallInteger)       # 状态
    start_ts = orm.Column(orm.Integer)      # 我方开始时间戳（整点）
    end_ts = orm.Column(orm.Integer)        # 我方结束时间戳（略早于官方）
    announce_ts = orm.Column(orm.Integer)   # 爬取到结果的时间戳
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)


class Trend(orm.Model):
    __tablename__ = "bj_pk10_trend"
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)
    number = orm.Column(orm.TEXT)
    idx_0 = orm.Column(orm.TEXT)  # 第一位
    idx_1 = orm.Column(orm.TEXT)  # 第二位
    idx_2 = orm.Column(orm.TEXT)  # 第三位
    idx_3 = orm.Column(orm.TEXT)  # 第四位
    idx_4 = orm.Column(orm.TEXT)  # 第五位
    idx_5 = orm.Column(orm.TEXT)
    idx_6 = orm.Column(orm.TEXT)
    idx_7 = orm.Column(orm.TEXT)
    idx_8 = orm.Column(orm.TEXT)
    idx_9 = orm.Column(orm.TEXT)
    # 前一到前五，直接使用idx；定位胆也是
    miss_7 = orm.Column(orm.TEXT)  # 1 bsoe
    miss_8 = orm.Column(orm.TEXT)  # 2 bsoe
    miss_9 = orm.Column(orm.TEXT)  # 3 bsoe
    miss_10 = orm.Column(orm.TEXT)  # 4 dt

    @classmethod
    def get_defaults(cls):
        data = {}
        for k in range(10):
            data['idx_%s' % k] = ','.join('x' * 10)

        data['miss_7'] = data['miss_8'] = data['miss_9'] = ','.join(['x'] * 4)
        data['miss_10'] = 'x,x'
        return data

    def get_stat(self, bet_type):
        ''' 根据玩法返回统计（遗漏、冷热、最大和上次）数据
            这个数据应该被缓存起来（因为冷热和最大需要实时计算）
            对于每一期而言，这个数据是固定的
        '''
        resp = {}
        if bet_type >= 7:
            resp['miss'] = getattr(self, 'miss_%s' % bet_type)
        else:
            d = []
            _max = bet_type if bet_type != 6 else 10
            for k in range(_max):
                d.append(getattr(self, 'idx_%s' % k))
            resp['miss'] = '|'.join(d)

        return resp
